from datetime import date, datetime, timedelta, timezone
from django.shortcuts import render
from django.db.models import Count, Max, Min, Q
from django.core.exceptions import ObjectDoesNotExist
from rest_framework import viewsets
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
from rest_framework.decorators import action
from rest_framework import status
from rest_framework import filters
from rest_framework.views import APIView
from rest_framework.schemas import AutoSchema, coreapi
from coreapi import Field
import coreschema
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from ephm_mqtt_data_push.utils import clear_tolerance_cache, update_tolerance_cache

from django_filters.rest_framework import DjangoFilterBackend

from .models import *
from .serializers import *
from ephm_sqlserver_api.models import Wertevar
import json
import re
import importlib
import requests
from rest_framework.schemas.coreapi import AutoSchema
import coreapi
import logging
from dateutil import parser

SERIALIZER_MODULE = "tms_manage.serializers"
MODULES = "tms_manage.models"

logger = logging.getLogger(__name__)


class get_station_info_viewset(APIView):
    page_size = 10

    schema = AutoSchema(
        manual_fields=[
            Field(
                name="api",
                required=True,
                location="query",
                schema=coreschema.String(description="接口名称,如: oil_level/data_hour/warning 等,采用模糊查询"),
            ),
            Field(
                name="area",
                required=False,
                location="query",
                schema=coreschema.String(description="区域,如: cb/ch 等,采用模糊查询"),
            ),
            Field(
                name="item",
                required=False,
                location="query",
                schema=coreschema.String(description="测点名称,如: tm_tlopen/SP_LOAD 等,采用模糊查询"),
            ),
        ],
    )

    def get(self, request, *args, **kwargs):
        station_info = self.get_queryset(request)
        if station_info:
            return Response(station_info, status=status.HTTP_200_OK)
        else:
            return Response("No data", status=status.HTTP_404_NOT_FOUND)

    def get_queryset(self, request, *args, **kwargs):
        api = request.query_params.get("api", None)
        area = request.query_params.get("area", None)
        item = request.query_params.get("item", None)

        if "data_hour" in api:
            print("data_hour_api")
            if area is None and item is not None:
                station_info = data_hour.objects.filter(item__icontains=item).values("station").distinct()
                station_info = queryset_formater(station_info)

            elif item is None and area is not None:
                station_info = data_hour.objects.filter(area__icontains=area).values("station").distinct()
                station_info = queryset_formater(station_info)

            elif area is not None and item is not None:
                station_info = data_hour.objects.filter(area__icontains=area, item__icontains=item).values("station").distinct()
                station_info = queryset_formater(station_info)

            else:
                station_info = data_hour.objects.none()

        elif "oil_level" in api:
            print("oil_level_api")
            if item is not None and area is None:
                area = area_formater(area)
                station_info = oil_level.objects.filter(item__icontains=item).values("station").distinct()
                station_info = queryset_formater(station_info)

            elif item is None and area is not None:
                area = area_formater(area)
                station_info = oil_level.objects.filter(station__icontains=area).values("station").distinct()
                station_info = queryset_formater(station_info)

            elif area is not None and item is not None:
                area = area_formater(area)
                station_info = oil_level.objects.filter(station__icontains=area, item__icontains=item).values("station").distinct()
                station_info = queryset_formater(station_info)

            else:
                station_info = oil_level.objects.none()

        elif "warning" in api:
            print("warning_api")
            if area is None and item is not None:
                station_info = warning.objects.filter(location__icontains=item).values("location").distinct()
                for i in station_info:
                    i["location"] = i["location"].split("-")[0]
                station_info = queryset_formater(station_info)

            elif item is None and area is not None:
                station_info = warning.objects.filter(area__icontains=area).values("location").distinct()
                for i in station_info:
                    i["location"] = i["location"].split("-")[0]
                station_info = queryset_formater(station_info)

            elif area is not None and item is not None:
                station_info = warning.objects.filter(area__icontains=area, location__icontains=item).values("location").distinct()
                for i in station_info:
                    i["location"] = i["location"].split("-")[0]
                station_info = queryset_formater(station_info)

            else:
                station_info = warning.objects.none()

        else:
            station_info = 0

        return station_info


class get_tool_nc_viewset(APIView):
    page_size = 10

    schema = AutoSchema(
        manual_fields=[
            Field(
                name="machine",
                required=True,
                location="query",
                schema=coreschema.String(description="设备名称,如: cb1op101,精确匹配"),
            ),
        ]
    )

    def get(self, request, *args, **kwargs):
        tool_nc_info = self.get_queryset(request)
        if tool_nc_info:
            return Response(tool_nc_info, status=status.HTTP_200_OK)
        else:
            return Response("No data", status=status.HTTP_404_NOT_FOUND)

    def get_queryset(self, request, *args, **kwargs):
        machine = request.query_params.get("machine", None)
        if machine is None:
            return None

        try:
            model = getattr(importlib.import_module(MODULES), machine)
        except Exception as e:
            print("get_queryset!!!", e)
            return None

        queryset = model.objects.all()

        tool_info = queryset.values("t_no").distinct()
        nc_info = queryset.values("nc_prg").distinct()

        tool_nc_info = {"tool_info": tool_info, "nc_info": nc_info}

        return tool_nc_info


def queryset_formater(queryset):  # 用于格式化queryset，去重，返回为前端所需下拉框数据格式
    data = []
    for i in queryset:
        if i.keys() == {"station"}:
            record = {"value": i["station"], "label": i["station"]}
            if record not in data:
                data.append(record)
        elif i.keys() == {"location"}:
            record = {"value": i["location"], "label": i["location"]}
            if record not in data:
                data.append(record)
    return data


def area_formater(area):  # 用于格式化area
    result = ""
    char = re.sub("([^\u0041-\u007a])", "", area)
    num = re.sub("([^\u0030-\u0039])", "", area)
    print(111111, char[-1].lower(), num)

    if char[-1].lower() != "m":
        print(222222)
        if "cs" in char:
            result = char + num
        else:
            result = char + "m" + num

    elif char[-1].lower() == "m":
        print(333333)
        if "cs" in char:
            result = char[:2]
        else:
            result = char + num

    print(result)
    return result


# ephm_limits表视图
class EphmLimitsCustomSchema(AutoSchema):
    """
    自定义的Schema类，用于处理EphmLimits相关的序列化逻辑。

    这个类继承自AutoSchema，并根据需要重写或添加方法以处理特定的序列化需求。

    示例用法：
    schema = EphmLimitsCustomSchema()
    serialized_data = schema.dump(data)
    """

    def get_manual_fields(self, path, method):
        # 获取父类中已有的手动字段
        manual_fields = super().get_manual_fields(path, method)

        if method.lower() == "get":
            # 为 GET 请求添加查询参数
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="query",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="zone",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="区域"),
                ),
                Field(
                    name="op",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="工位号"),
                ),
                Field(
                    name="item",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="测点"),
                ),
                Field(
                    name="item_cn",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="测点中文名"),
                ),
                Field(
                    name="item_level",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="测点等级"),
                ),
                Field(
                    name="up_limit",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="上限"),
                ),
                Field(
                    name="low_limit",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="下限"),
                ),
                Field(name="changetime", required=False, location="query", schema=coreschema.String(description="修改日期时间(例如:2025-01-16 16:39:01)")),
                # 更多查询参数...
            ]
        elif method.lower() == "post":
            # 为 POST 请求设置必填项和其他选项
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="zone",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="区域"),
                ),
                Field(
                    name="op",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="工位号"),
                ),
                Field(
                    name="item",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="测点"),
                ),
                Field(
                    name="item_cn",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="测点中文名"),
                ),
                Field(
                    name="item_level",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="测点等级"),
                ),
                Field(
                    name="up_limit",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="上限"),
                ),
                Field(
                    name="low_limit",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="下限"),
                ),
            ]
        elif method.lower() == "patch":
            manual_fields = [
                Field(
                    name="id",
                    required=True,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="zone",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="区域"),
                ),
                Field(
                    name="op",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="工位号"),
                ),
                Field(
                    name="item",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="测点"),
                ),
                Field(
                    name="item_cn",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="测点中文名"),
                ),
                Field(
                    name="item_level",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="测点等级"),
                ),
                Field(
                    name="up_limit",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="上限"),
                ),
                Field(
                    name="low_limit",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="下限"),
                ),
            ]
        elif method.lower() == "delete":
            manual_fields = [
                Field(
                    name="id",
                    required=True,
                    location="query",
                    schema=coreschema.Number(description="id"),
                )
            ]
        return manual_fields


class EphmLimitsView(APIView):
    """
    类说明：
        用于处理ephm_limits表的数据，可以使用get，post，patch，delete方法
    方法：
        get:获取数据
        post:添加数据
        patch:部分更新数据
        delete:删除数据
    """

    # 设置默认的每页默认数据量
    page_size = 100000
    # 设置在API DOC的字段显示
    schema = EphmLimitsCustomSchema()

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = EphmLimits_Serializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def post(self, request, *args, **kwargs):
        request.data["changetime"] = datetime.now()
        serializer = EphmLimits_Serializer(data=request.data)

        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def patch(self, request, *args, **kwargs):
        id = request.data["id"]
        try:
            instance = EphmLimits.objects.get(id=id)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=404)

        serializer = EphmLimits_Serializer(instance, data=request.data, partial=True)
        if serializer.is_valid():
            instance.changetime = datetime.now()
            serializer.save()
            return Response(serializer.data, status=200)
        return Response(serializer.errors, status=400)

    def delete(self, request, *args, **kwargs):
        id = request.query_params.get("id")
        try:
            instance = EphmLimits.objects.get(id=id)
            instance.delete()
            return Response({"message": "删除成功！"}, status=status.HTTP_200_OK)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=status.HTTP_404_NOT_FOUND)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        queryset = EphmLimits.objects.filter().order_by("id")
        # 获取请求的参数
        id = request.query_params.get("id")
        zone = request.query_params.get("zone")
        op = request.query_params.get("op")
        item = request.query_params.get("item")
        item_cn = request.query_params.get("item_cn")
        item_level = request.query_params.get("item_level")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if zone is not None:
            queryset = queryset.filter(zone=zone)
        if op is not None:
            queryset = queryset.filter(op=op)
        if item is not None:
            queryset = queryset.filter(item=item)
        if item_cn is not None:
            queryset = queryset.filter(item_cn=item_cn)
        if item_level is not None:
            queryset = queryset.filter(item_level=item_level)
        else:
            pass
        return queryset


# ephm_limits_warning表视图
class EphmLimitsWarningCustomSchema(AutoSchema):
    """
    自定义的Schema类，用于处理EphmLimitsWarning相关的序列化逻辑。

    这个类继承自AutoSchema，并根据需要重写或添加方法以处理特定的序列化需求。

    """

    def get_manual_fields(self, path, method):
        # 获取父类中已有的手动字段
        manual_fields = super().get_manual_fields(path, method)

        if method.lower() == "get":
            # 为 GET 请求添加查询参数
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="query",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="zone",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="区域"),
                ),
                Field(
                    name="op",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="工位号"),
                ),
                Field(
                    name="item",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="测点"),
                ),
                Field(
                    name="item_cn",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="测点中文名"),
                ),
                Field(
                    name="item_level",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="测点等级"),
                ),
                Field(
                    name="value",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="超限报警值"),
                ),
                Field(
                    name="up_limit",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="上限"),
                ),
                Field(
                    name="low_limit",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="下限"),
                ),
                Field(
                    name="measures",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="措施"),
                ),
                Field(
                    name="person_in_charge",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="负责人"),
                ),
                Field(
                    name="start_time",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01 16:34:42"),
                ),
                Field(
                    name="end_time",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10 16:34:42"),
                ),
                # 更多查询参数...
            ]
        elif method.lower() == "post":
            # 为 POST 请求设置必填项和其他选项
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="zone",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="区域"),
                ),
                Field(
                    name="op",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="工位号"),
                ),
                Field(
                    name="item",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="测点"),
                ),
                Field(
                    name="item_cn",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="测点中文名"),
                ),
                Field(
                    name="item_level",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="测点等级"),
                ),
                Field(
                    name="value",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="超限报警值"),
                ),
                Field(
                    name="up_limit",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="上限"),
                ),
                Field(
                    name="low_limit",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="下限"),
                ),
                Field(
                    name="ephm_measures",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="措施"),
                ),
                Field(
                    name="person_in_charge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="负责人"),
                ),
            ]
        elif method.lower() == "patch":
            manual_fields = [
                Field(
                    name="id",
                    required=True,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="zone",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="区域"),
                ),
                Field(
                    name="op",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="工位号"),
                ),
                Field(
                    name="item",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="测点"),
                ),
                Field(
                    name="item_cn",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="测点中文名"),
                ),
                Field(
                    name="item_level",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="测点等级"),
                ),
                Field(
                    name="value",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="超限报警值"),
                ),
                Field(
                    name="up_limit",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="上限"),
                ),
                Field(
                    name="low_limit",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="下限"),
                ),
                Field(
                    name="ephm_measures",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="措施"),
                ),
                Field(
                    name="person_in_charge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="负责人"),
                ),
            ]
        elif method.lower() == "delete":
            manual_fields = [
                Field(
                    name="id",
                    required=True,
                    location="query",
                    schema=coreschema.Number(description="id"),
                )
            ]
        return manual_fields


class EphmLimitsWarningView(APIView):
    """
    类说明：
        用于处理ephm_limits_warning表的数据，可以使用get，post，patch，delete方法
    方法：
        get:获取数据
        post:添加数据
        patch:部分更新数据
        delete:删除数据
    """

    # 设置默认的每页默认数据量
    page_size = 100000
    # 设置在API DOC的字段显示
    schema = EphmLimitsWarningCustomSchema()

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = EphmLimitsWarning_Serializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def post(self, request, *args, **kwargs):
        request.data["timestamp"] = datetime.now()
        request.data["create_time"] = datetime.now()
        request.data["update_time"] = datetime.now()
        serializer = EphmLimitsWarning_Serializer(data=request.data)

        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def patch(self, request, *args, **kwargs):
        id = request.data["id"]
        try:
            instance = EphmLimitsWarning.objects.get(id=id)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=404)
        request.data["update_time"] = datetime.now()
        serializer = EphmLimitsWarning_Serializer(instance, data=request.data, partial=True)
        if serializer.is_valid():
            instance.update_time = datetime.now()
            serializer.save()
            return Response(serializer.data, status=200)
        return Response(serializer.errors, status=400)

    def delete(self, request, *args, **kwargs):
        id = request.query_params.get("id")
        try:
            instance = EphmLimitsWarning.objects.get(id=id)
            instance.delete()
            return Response({"message": "删除成功！"}, status=status.HTTP_200_OK)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=status.HTTP_404_NOT_FOUND)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        queryset = EphmLimitsWarning.objects.filter().order_by("id")
        # 获取请求的参数
        id = request.query_params.get("id")
        zone = request.query_params.get("zone")
        op = request.query_params.get("op")
        item = request.query_params.get("item")
        item_cn = request.query_params.get("item_cn")
        ephm_measures = request.query_params.get("ephm_measures")
        person_in_charge = request.query_params.get("person_in_charge")
        item_level = request.query_params.get("item_level")
        start_time_param = request.query_params.get("start_time", None)
        end_time_param = request.query_params.get("end_time", None)
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if zone is not None:
            queryset = queryset.filter(zone=zone)
        if op is not None:
            queryset = queryset.filter(op=op)
        if item is not None:
            queryset = queryset.filter(item=item)
        if item_cn is not None:
            queryset = queryset.filter(item_cn=item_cn)
        if item_level is not None:
            queryset = queryset.filter(item_level=item_level)
        if ephm_measures is not None:
            queryset = queryset.filter(ephm_measures__contains=ephm_measures)
        if person_in_charge is not None:
            queryset = queryset.filter(person_in_charge__contains=person_in_charge)
        if (start_time_param and end_time_param) is not None:
            start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d %H:%M:%S")
            end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d %H:%M:%S")
            print(start_time_param_trans)
            print(end_time_param_trans)
            queryset = queryset.filter(timestamp__gte=start_time_param_trans, timestamp__lte=end_time_param_trans)
        else:
            pass
        return queryset



# ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓重庆模块视图函数↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

# tms系统库存管理表视图

class TmsInventoryCustomSchema(AutoSchema):
    """
    自定义的Schema类，用于处理TmsInventory相关的序列化逻辑。

    这个类继承自AutoSchema，并根据需要重写或添加方法以处理特定的序列化需求。

    示例用法：
    schema = TmsInventoryCustomSchema()
    serialized_data = schema.dump(data)
    """

    def get_manual_fields(self, path, method):
        # 获取父类中已有的手动字段
        manual_fields = super().get_manual_fields(path, method)

        if method.lower() == "get":
            # 为 GET 请求添加查询参数
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="query",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="process_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="工序号"),
                ),
                Field(
                    name="storage_location",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="仓库位置"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="add_inventory_time", 
                    required=False, 
                    location="query", 
                    schema=coreschema.String(description="入库时间(例如:2025-01-16 16:39:01)"),
                ),
                # 更多查询参数...
            ]
        elif method.lower() == "post":
            # 为 POST 请求设置必填项和其他选项
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="process_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="工序号"),
                ),
                Field(
                    name="storage_location",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="仓库位置"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="tool_unit_price",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具单价"),
                ),
                Field(
                    name="inventory_amount",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="库存金额"),
                ),
                Field(
                    name="add_inventory_time", 
                    required=False, 
                    location="form", 
                    schema=coreschema.String(description="入库时间(例如:2025-01-16 16:39:01)"),
                ),
            ]
        elif method.lower() == "patch":
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="process_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="工序号"),
                ),
                Field(
                    name="storage_location",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="仓库位置"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="tool_unit_price",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具单价"),
                ),
                Field(
                    name="inventory_amount",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="库存金额"),
                ),
                Field(
                    name="add_inventory_time", 
                    required=False, 
                    location="form", 
                    schema=coreschema.String(description="入库时间(例如:2025-01-16 16:39:01)"),
                ),
            ]
        elif method.lower() == "delete":
            manual_fields = [
                Field(
                    name="id",
                    required=True,
                    location="query",
                    schema=coreschema.Number(description="id"),
                )
            ]
        return manual_fields

class TmsInventoryView(APIView):
    """
    类说明：
        用于处理TmsInventory表的数据，可以使用get，post，patch，delete方法
    方法：
        get:获取数据
        post:添加数据
        patch:部分更新数据
        delete:删除数据
    """

    # 设置默认的每页默认数据量
    page_size = 100000
    # 设置在API DOC的字段显示
    schema = TmsInventoryCustomSchema()

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = TmsInventorySerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def post(self, request, *args, **kwargs):
        request.data["add_inventory_time"] = datetime.now()
        serializer = TmsInventorySerializer(data=request.data)

        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def patch(self, request, *args, **kwargs):
        id = request.data["id"]
        print(request.data)
        try:
            instance = TmsInventory.objects.get(id=id)
            print(instance)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=404)

        serializer = TmsInventorySerializer(instance, data=request.data, partial=True)
        if serializer.is_valid():
            print("有效")
            serializer.save()
            instance.last_out_invent_time = datetime.now()
            instance.save()
            return Response(serializer.data, status=200)
        else :
            print("无效")
            return Response(serializer.errors, status=400)

    def delete(self, request, *args, **kwargs):
        id = request.query_params.get("id")
        try:
            instance = TmsInventory.objects.get(id=id)
            instance.delete()
            return Response({"message": "删除成功！"}, status=status.HTTP_200_OK)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=status.HTTP_404_NOT_FOUND)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        queryset = TmsInventory.objects.filter().order_by("id")
        # 获取请求的参数
        id = request.query_params.get("id")
        prline = request.query_params.get("prline")
        supplier = request.query_params.get("supplier")
        tool_number = request.query_params.get("tool_number")
        tool_type = request.query_params.get("tool_type")
        tool_status = request.query_params.get("tool_status")
        process_number = request.query_params.get("process_number")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if prline is not None:
            queryset = queryset.filter(prline=prline)
        if supplier is not None:
            queryset = queryset.filter(supplier=supplier)
        if tool_number is not None:
            queryset = queryset.filter(tool_number=tool_number)
        if tool_type is not None:
            queryset = queryset.filter(tool_type=tool_type)
        if tool_status is not None:
            queryset = queryset.filter(tool_status=tool_status)
        if process_number is not None:
            queryset = queryset.filter(process_number=process_number)
        else:
            pass
        return queryset

# 入库记录表视图

class TmsAddInventoryCustomSchema(AutoSchema):
    """
    自定义的Schema类，用于处理TmsInventory相关的序列化逻辑。

    这个类继承自AutoSchema，并根据需要重写或添加方法以处理特定的序列化需求。

    示例用法：
    schema = TmsInventoryCustomSchema()
    serialized_data = schema.dump(data)
    """

    def get_manual_fields(self, path, method):
        # 获取父类中已有的手动字段
        manual_fields = super().get_manual_fields(path, method)

        if method.lower() == "get":
            # 为 GET 请求添加查询参数
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="query",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="process_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="工序号"),
                ),
                Field(
                    name="storage_location",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="仓库位置"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="add_inventory_time", 
                    required=False, 
                    location="query", 
                    schema=coreschema.String(description="入库时间(例如:2025-01-16 16:39:01)"),
                ),
                # 更多查询参数...
            ]
        elif method.lower() == "post":
            # 为 POST 请求设置必填项和其他选项
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="process_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="工序号"),
                ),
                Field(
                    name="storage_location",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="仓库位置"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="add_inventory_time", 
                    required=False, 
                    location="form", 
                    schema=coreschema.String(description="入库时间(例如:2025-01-16 16:39:01)"),
                ),
            ]
        elif method.lower() == "patch":
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="process_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="工序号"),
                ),
                Field(
                    name="storage_location",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="仓库位置"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="add_inventory_time", 
                    required=False, 
                    location="form", 
                    schema=coreschema.String(description="入库时间(例如:2025-01-16 16:39:01)"),
                ),
            ]
        elif method.lower() == "delete":
            manual_fields = [
                Field(
                    name="id",
                    required=True,
                    location="query",
                    schema=coreschema.Number(description="id"),
                )
            ]
        return manual_fields

class TmsAddInventoryLogView(APIView):
    """
    类说明：
        用于处理tms_add_inventory_log表的数据，可以使用get，post，patch，delete方法
    方法：
        get:获取数据
        post:添加数据
        patch:部分更新数据
        delete:删除数据
    """

    # 设置默认的每页默认数据量
    page_size = 100000
    # 设置在API DOC的字段显示
    schema = TmsAddInventoryCustomSchema()

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = TmsAddInventorySerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def post(self, request, *args, **kwargs):
        request.data["add_inventory_time"] = datetime.now()
        serializer = TmsAddInventorySerializer(data=request.data)

        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def patch(self, request, *args, **kwargs):
        id = request.data["id"]
        try:
            instance = TmsAddInventoryLog.objects.get(id=id)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=404)

        serializer = TmsAddInventorySerializer(instance, data=request.data, partial=True)
        if serializer.is_valid():
            instance.add_inventory_time = datetime.now()
            serializer.save()
            return Response(serializer.data, status=200)
        return Response(serializer.errors, status=400)

    def delete(self, request, *args, **kwargs):
        id = request.query_params.get("id")
        try:
            instance = TmsAddInventoryLog.objects.get(id=id)
            instance.delete()
            return Response({"message": "删除成功！"}, status=status.HTTP_200_OK)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=status.HTTP_404_NOT_FOUND)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        queryset = TmsAddInventoryLog.objects.filter().order_by("id")
        # 获取请求的参数
        id = request.query_params.get("id")
        prline = request.query_params.get("prline")
        supplier = request.query_params.get("supplier")
        tool_number = request.query_params.get("tool_number")
        tool_type = request.query_params.get("tool_type")
        process_number = request.query_params.get("process_number")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if prline is not None:
            queryset = queryset.filter(prline=prline)
        if supplier is not None:
            queryset = queryset.filter(supplier=supplier)
        if tool_number is not None:
            queryset = queryset.filter(tool_number=tool_number)
        if tool_type is not None:
            queryset = queryset.filter(tool_type=tool_type)
        if process_number is not None:
            queryset = queryset.filter(process_number=process_number)
        else:
            pass
        return queryset

# 出库记录表视图
class TmsOutInventoryCustomSchema(AutoSchema):
    """
    自定义的Schema类，用于处理TmsInventory相关的序列化逻辑。

    这个类继承自AutoSchema，并根据需要重写或添加方法以处理特定的序列化需求。

    示例用法：
    schema = TmsInventoryCustomSchema()
    serialized_data = schema.dump(data)
    """

    def get_manual_fields(self, path, method):
        # 获取父类中已有的手动字段
        manual_fields = super().get_manual_fields(path, method)

        if method.lower() == "get":
            # 为 GET 请求添加查询参数
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="query",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="process_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="工序号"),
                ),
                Field(
                    name="storage_location",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="仓库位置"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="out_inventory_time", 
                    required=False, 
                    location="query", 
                    schema=coreschema.String(description="出库时间(例如:2025-01-16 16:39:01)"),
                ),
                # 更多查询参数...
            ]
        elif method.lower() == "post":
            # 为 POST 请求设置必填项和其他选项
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="process_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="工序号"),
                ),
                Field(
                    name="storage_location",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="仓库位置"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="add_inventory_time", 
                    required=False, 
                    location="form", 
                    schema=coreschema.String(description="入库时间(例如:2025-01-16 16:39:01)"),
                ),
            ]
        elif method.lower() == "patch":
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="process_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="工序号"),
                ),
                Field(
                    name="storage_location",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="仓库位置"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="add_inventory_time", 
                    required=False, 
                    location="form", 
                    schema=coreschema.String(description="入库时间(例如:2025-01-16 16:39:01)"),
                ),
            ]
        elif method.lower() == "delete":
            manual_fields = [
                Field(
                    name="id",
                    required=True,
                    location="query",
                    schema=coreschema.Number(description="id"),
                )
            ]
        return manual_fields

class TmsOutInventoryLogView(APIView):
    """
    类说明：
        用于处理tms_out_inventory_log表的数据，可以使用get，post，patch，delete方法
    方法：
        get:获取数据
        post:添加数据
        patch:部分更新数据
        delete:删除数据
    """

    # 设置默认的每页默认数据量
    page_size = 100000
    # 设置在API DOC的字段显示
    schema = TmsOutInventoryCustomSchema()

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = TmsOutInventorySerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def post(self, request, *args, **kwargs):
        request.data["add_inventory_time"] = datetime.now()
        serializer = TmsOutInventorySerializer(data=request.data)

        if serializer.is_valid():
            serializer.save()
            instance = TmsOutInventoryLog.objects.get(id=serializer.data["id"])
            instance.out_inventory_time = datetime.now()
            instance.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def patch(self, request, *args, **kwargs):
        id = request.data["id"]
        try:
            instance = TmsOutInventoryLog.objects.get(id=id)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=404)

        serializer = TmsOutInventorySerializer(instance, data=request.data, partial=True)
        if serializer.is_valid():
            instance.add_inventory_time = datetime.now()
            serializer.save()
            return Response(serializer.data, status=200)
        return Response(serializer.errors, status=400)

    def delete(self, request, *args, **kwargs):
        id = request.query_params.get("id")
        try:
            instance = TmsOutInventoryLog.objects.get(id=id)
            instance.delete()
            return Response({"message": "删除成功！"}, status=status.HTTP_200_OK)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=status.HTTP_404_NOT_FOUND)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        queryset = TmsOutInventoryLog.objects.filter().order_by("id")
        # 获取请求的参数
        id = request.query_params.get("id")
        prline = request.query_params.get("prline")
        supplier = request.query_params.get("supplier")
        tool_number = request.query_params.get("tool_number")
        tool_type = request.query_params.get("tool_type")
        process_number = request.query_params.get("process_number")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if prline is not None:
            queryset = queryset.filter(prline=prline)
        if supplier is not None:
            queryset = queryset.filter(supplier=supplier)
        if tool_number is not None:
            queryset = queryset.filter(tool_number=tool_number)
        if tool_type is not None:
            queryset = queryset.filter(tool_type=tool_type)
        if process_number is not None:
            queryset = queryset.filter(process_number=process_number)
        else:
            pass
        return queryset

# 物料信息表视图
class TmsMaterialInfoCustomSchema(AutoSchema):
    """
    自定义的Schema类，用于处理TmsMaterialInfo相关的序列化逻辑。

    这个类继承自AutoSchema，并根据需要重写或添加方法以处理特定的序列化需求。

    示例用法：
    schema = TmsInventoryCustomSchema()
    serialized_data = schema.dump(data)
    """

    def get_manual_fields(self, path, method):
        # 获取父类中已有的手动字段
        manual_fields = super().get_manual_fields(path, method)

        if method.lower() == "get":
            # 为 GET 请求添加查询参数
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="query",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="purchase_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="采购号"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="material_add_time", 
                    required=False, 
                    location="query", 
                    schema=coreschema.String(description="物料信息添加时间(例如:2025-01-16 16:39:01)"),
                ),
                # 更多查询参数...
            ]
        elif method.lower() == "post":
            # 为 POST 请求设置必填项和其他选项
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="purchase_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="采购号"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="material_add_time", 
                    required=False,
                    location="form",
                    schema=coreschema.String(description="物料信息添加时间(例如:2025-01-16 16:39:01)"),
                ),
            ]
        elif method.lower() == "patch":
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="purchase_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="采购号"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="material_add_time", 
                    required=False,
                    location="form",
                    schema=coreschema.String(description="物料信息添加时间(例如:2025-01-16 16:39:01)"),
                ),
            ]
        elif method.lower() == "delete":
            manual_fields = [
                Field(
                    name="id",
                    required=True,
                    location="query",
                    schema=coreschema.Number(description="id"),
                )
            ]
        return manual_fields

class TmsMaterialInfoView(APIView):
    """
    类说明：
        用于处理TmsMaterialInfo表的数据，可以使用get，post，patch，delete方法
    方法：
        get:获取数据
        post:添加数据
        patch:部分更新数据
        delete:删除数据
    """

    # 设置默认的每页默认数据量
    page_size = 100000
    # 设置在API DOC的字段显示
    schema = TmsMaterialInfoCustomSchema()

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = TmsMaterialInfoSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def post(self, request, *args, **kwargs):
        request.data["add_inventory_time"] = datetime.now()
        serializer = TmsMaterialInfoSerializer(data=request.data)

        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def patch(self, request, *args, **kwargs):
        id = request.data["id"]
        try:
            instance = TmsMaterialInfo.objects.get(id=id)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=404)

        serializer = TmsMaterialInfoSerializer(instance, data=request.data, partial=True)
        if serializer.is_valid():
            instance.material_add_time = datetime.now()
            serializer.save()
            return Response(serializer.data, status=200)
        return Response(serializer.errors, status=400)

    def delete(self, request, *args, **kwargs):
        id = request.query_params.get("id")
        try:
            instance = TmsMaterialInfo.objects.get(id=id)
            instance.delete()
            return Response({"message": "删除成功！"}, status=status.HTTP_200_OK)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=status.HTTP_404_NOT_FOUND)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        queryset = TmsMaterialInfo.objects.filter().order_by("id")
        # 获取请求的参数
        id = request.query_params.get("id")
        prline = request.query_params.get("prline")
        purchase_number=request.query_params.get("purchase_number")
        supplier = request.query_params.get("supplier")
        tool_number = request.query_params.get("tool_number")
        tool_life = request.query_params.get("tool_life")
        tool_type = request.query_params.get("tool_type")
        teeth_number = request.query_params.get("teeth_number")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if prline is not None:
            queryset = queryset.filter(prline=prline)
        if purchase_number is not None:
            queryset = queryset.filter(purchase_number=purchase_number)
        if supplier is not None:
            queryset = queryset.filter(supplier=supplier)
        if tool_number is not None:
            queryset = queryset.filter(tool_number=tool_number)
        if tool_life is not None:
            queryset = queryset.filter(tool_life=tool_life)
        if tool_type is not None:
            queryset = queryset.filter(tool_type=tool_type)
        if teeth_number is not None:
            queryset = queryset.filter(teeth_number=teeth_number)
        else:
            pass
        return queryset

# 采购表视图

class TmsPurchaseListCustomSchema(AutoSchema):
    """
    自定义的Schema类，用于处理TmsPurchaseList相关的序列化逻辑。

    这个类继承自AutoSchema，并根据需要重写或添加方法以处理特定的序列化需求。
    """

    def get_manual_fields(self, path, method):
        # 获取父类中已有的手动字段
        manual_fields = super().get_manual_fields(path, method)

        if method.lower() == "get":
            # 为 GET 请求添加查询参数
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="query",
                    schema=coreschema.Number(description="id"),
                ),
                
                Field(
                    name="supplier",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="purchase_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="采购号"),
                ),
                Field(
                    name="bak_purchase_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="备用采购号"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具型号"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="lead_time",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="交货周期"),
                ),
                Field(
                    name="safety_factor",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="安全系数"),
                ),
                Field(
                    name="approval_time",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="审批时间"),
                ),Field(
                    name="m_replace_q",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="月更换数量"),
                ),
                Field(
                    name="min_invent_q",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="最小库存数量"),
                ),
                Field(
                    name="actual_tool_life",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="实际刀具寿命"),
                ),
                Field(
                    name="purchase_time", 
                    required=False, 
                    location="query", 
                    schema=coreschema.String(description="发起采购时间(例如:2025-01-16 16:39:01)"),
                ),
                Field(
                    name="purchase_quantity",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="采购数量"),
                ),
                Field(
                    name="purchase_status",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="采购状态"),
                ),
                
                # 更多查询参数...
            ]
        elif method.lower() == "post":
            # 为 POST 请求设置必填项和其他选项
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                
                Field(
                    name="supplier",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="purchase_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="采购号"),
                ),
                Field(
                    name="bak_purchase_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备用采购号"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具型号"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="lead_time",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="交货周期"),
                ),
                Field(
                    name="safety_factor",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="安全系数"),
                ),
                Field(
                    name="approval_time",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="审批时间"),
                ),Field(
                    name="m_replace_q",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="月更换数量"),
                ),
                Field(
                    name="min_invent_q",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="最小库存数量"),
                ),
                Field(
                    name="actual_tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="实际刀具寿命"),
                ),
                Field(
                    name="purchase_time", 
                    required=False, 
                    location="form", 
                    schema=coreschema.String(description="发起采购时间(例如:2025-01-16 16:39:01)"),
                ),
                Field(
                    name="purchase_quantity",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="采购数量"),
                ),
                Field(
                    name="purchase_status",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="采购状态"),
                ),
            ]
        elif method.lower() == "patch":
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                
                Field(
                    name="supplier",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="purchase_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="采购号"),
                ),
                Field(
                    name="bak_purchase_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备用采购号"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具型号"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="lead_time",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="交货周期"),
                ),
                Field(
                    name="safety_factor",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="安全系数"),
                ),
                Field(
                    name="approval_time",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="审批时间"),
                ),Field(
                    name="m_replace_q",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="月更换数量"),
                ),
                Field(
                    name="min_invent_q",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="最小库存数量"),
                ),
                Field(
                    name="actual_tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="实际刀具寿命"),
                ),
                Field(
                    name="purchase_time", 
                    required=False, 
                    location="form", 
                    schema=coreschema.String(description="发起采购时间(例如:2025-01-16 16:39:01)"),
                ),
                Field(
                    name="purchase_quantity",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="采购数量"),
                ),
                Field(
                    name="purchase_status",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="采购状态"),
                ),
            ]
        elif method.lower() == "delete":
            manual_fields = [
                Field(
                    name="id",
                    required=True,
                    location="query",
                    schema=coreschema.Number(description="id"),
                )
            ]
        return manual_fields

class TmsPurchaseListView(APIView):
    """
    类说明：
        用于处理TmsPurchaseList表的数据，可以使用get，post，patch，delete方法
    方法：
        get:获取数据
        post:添加数据
        patch:部分更新数据
        delete:删除数据
    """

    # 设置默认的每页默认数据量
    page_size = 100000
    # 设置在API DOC的字段显示
    schema = TmsPurchaseListCustomSchema()

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = TmsPurchaseListSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def post(self, request, *args, **kwargs):
        request.data["purchase_time"] = datetime.now()
        serializer = TmsPurchaseListSerializer(data=request.data)

        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def patch(self, request, *args, **kwargs):
        id = request.data["id"]
        try:
            instance = TmsPurchaseList.objects.get(id=id)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=404)

        serializer = TmsPurchaseListSerializer(instance, data=request.data, partial=True)
        if serializer.is_valid():
            # instance.material_add_time = datetime.now()
            serializer.save()
            return Response(serializer.data, status=200)
        return Response(serializer.errors, status=400)

    def delete(self, request, *args, **kwargs):
        id = request.query_params.get("id")
        try:
            instance = TmsPurchaseList.objects.get(id=id)
            instance.delete()
            return Response({"message": "删除成功！"}, status=status.HTTP_200_OK)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=status.HTTP_404_NOT_FOUND)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        queryset = TmsPurchaseList.objects.filter().order_by("id")
        # 获取请求的参数
        id = request.query_params.get("id")
        supplier = request.query_params.get("supplier")
        purchase_number=request.query_params.get("purchase_number")
        bak_purchase_number = request.query_params.get("bak_purchase_number")
        tool_number = request.query_params.get("tool_number")
        tool_type = request.query_params.get("tool_type")
        tool_life = request.query_params.get("tool_life")
        tool_status = request.query_params.get("tool_status")
        cutting_edge = request.query_params.get("cutting_edge")
        teeth_number = request.query_params.get("teeth_number")
        purchase_status = request.query_params.get("purchase_status")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if supplier is not None:
            queryset = queryset.filter(supplier=supplier)
        if purchase_number is not None:
            queryset = queryset.filter(purchase_number=purchase_number)
        if bak_purchase_number is not None:
            queryset = queryset.filter(bak_purchase_number=bak_purchase_number)
        if tool_number is not None:
            queryset = queryset.filter(tool_number=tool_number)
        if tool_type is not None:
            queryset = queryset.filter(tool_type=tool_type)
        if tool_life is not None:
            queryset = queryset.filter(tool_life=tool_life)
        if tool_status is not None:
            queryset = queryset.filter(tool_status=tool_status)
        if cutting_edge is not None:
            queryset = queryset.filter(cutting_edge=cutting_edge)
        if teeth_number is not None:
            queryset = queryset.filter(teeth_number=teeth_number)
        if purchase_status is not None:
            queryset = queryset.filter(purchase_status=purchase_status)
        else:
            pass
        return queryset


#↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑重庆模块视图函数↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑




# ↓↓↓↓↓↓↓↓↓↓↓↓↓↓青岛模块视图函数↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

# tms系统库存管理表视图

class TmsInventoryQdCustomSchema(AutoSchema):
    """
    自定义的Schema类，用于处理TmsInventoryQd相关的序列化逻辑。

    这个类继承自AutoSchema，并根据需要重写或添加方法以处理特定的序列化需求。

    示例用法：
    schema = TmsInventoryCustomSchema()
    serialized_data = schema.dump(data)
    """

    def get_manual_fields(self, path, method):
        # 获取父类中已有的手动字段
        manual_fields = super().get_manual_fields(path, method)

        if method.lower() == "get":
            # 为 GET 请求添加查询参数
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="query",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="process_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="工序号"),
                ),
                Field(
                    name="storage_location",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="仓库位置"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="add_inventory_time", 
                    required=False, 
                    location="query", 
                    schema=coreschema.String(description="入库时间(例如:2025-01-16 16:39:01)"),
                ),
                # 更多查询参数...
            ]
        elif method.lower() == "post":
            # 为 POST 请求设置必填项和其他选项
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="process_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="工序号"),
                ),
                Field(
                    name="storage_location",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="仓库位置"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="add_inventory_time", 
                    required=False, 
                    location="form", 
                    schema=coreschema.String(description="入库时间(例如:2025-01-16 16:39:01)"),
                ),
            ]
        elif method.lower() == "patch":
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="process_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="工序号"),
                ),
                Field(
                    name="storage_location",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="仓库位置"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="add_inventory_time", 
                    required=False, 
                    location="form", 
                    schema=coreschema.String(description="入库时间(例如:2025-01-16 16:39:01)"),
                ),
            ]
        elif method.lower() == "delete":
            manual_fields = [
                Field(
                    name="id",
                    required=True,
                    location="query",
                    schema=coreschema.Number(description="id"),
                )
            ]
        return manual_fields

class TmsInventoryQdView(APIView):
    """
    类说明：
        用于处理TmsInventory表的数据，可以使用get，post，patch，delete方法
    方法：
        get:获取数据
        post:添加数据
        patch:部分更新数据
        delete:删除数据
    """

    # 设置默认的每页默认数据量
    page_size = 100000
    # 设置在API DOC的字段显示
    schema = TmsInventoryQdCustomSchema()

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = TmsInventoryQdSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def post(self, request, *args, **kwargs):
        request.data["add_inventory_time"] = datetime.now()
        serializer = TmsInventoryQdSerializer(data=request.data)

        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def patch(self, request, *args, **kwargs):
        id = request.data["id"]
        print(request.data)
        try:
            instance = TmsInventoryQd.objects.get(id=id)
            print(instance)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=404)

        serializer = TmsInventoryQdSerializer(instance, data=request.data, partial=True)
        if serializer.is_valid():
            print("有效")
            serializer.save()
            instance.last_out_invent_time = datetime.now()
            instance.save()
            return Response(serializer.data, status=200)
        else :
            print("无效")
            return Response(serializer.errors, status=400)

    def delete(self, request, *args, **kwargs):
        id = request.query_params.get("id")
        try:
            instance = TmsInventoryQd.objects.get(id=id)
            instance.delete()
            return Response({"message": "删除成功！"}, status=status.HTTP_200_OK)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=status.HTTP_404_NOT_FOUND)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        queryset = TmsInventoryQd.objects.filter().order_by("id")
        # 获取请求的参数
        id = request.query_params.get("id")
        prline = request.query_params.get("prline")
        supplier = request.query_params.get("supplier")
        tool_number = request.query_params.get("tool_number")
        tool_type = request.query_params.get("tool_type")
        tool_status = request.query_params.get("tool_status")
        process_number = request.query_params.get("process_number")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if prline is not None:
            queryset = queryset.filter(prline=prline)
        if supplier is not None:
            queryset = queryset.filter(supplier=supplier)
        if tool_number is not None:
            queryset = queryset.filter(tool_number=tool_number)
        if tool_type is not None:
            queryset = queryset.filter(tool_type=tool_type)
        if tool_status is not None:
            queryset = queryset.filter(tool_status=tool_status)
        if process_number is not None:
            queryset = queryset.filter(process_number=process_number)
        else:
            pass
        return queryset

# 入库记录表视图

class TmsAddInventoryQdCustomSchema(AutoSchema):
    """
    自定义的Schema类，用于处理TmsInventoryQd相关的序列化逻辑。

    这个类继承自AutoSchema，并根据需要重写或添加方法以处理特定的序列化需求。

    示例用法：
    schema = TmsInventoryQdCustomSchema()
    serialized_data = schema.dump(data)
    """

    def get_manual_fields(self, path, method):
        # 获取父类中已有的手动字段
        manual_fields = super().get_manual_fields(path, method)

        if method.lower() == "get":
            # 为 GET 请求添加查询参数
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="query",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="process_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="工序号"),
                ),
                Field(
                    name="storage_location",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="仓库位置"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="add_inventory_time", 
                    required=False, 
                    location="query", 
                    schema=coreschema.String(description="入库时间(例如:2025-01-16 16:39:01)"),
                ),
                # 更多查询参数...
            ]
        elif method.lower() == "post":
            # 为 POST 请求设置必填项和其他选项
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="process_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="工序号"),
                ),
                Field(
                    name="storage_location",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="仓库位置"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="add_inventory_time", 
                    required=False, 
                    location="form", 
                    schema=coreschema.String(description="入库时间(例如:2025-01-16 16:39:01)"),
                ),
            ]
        elif method.lower() == "patch":
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="process_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="工序号"),
                ),
                Field(
                    name="storage_location",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="仓库位置"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="add_inventory_time", 
                    required=False, 
                    location="form", 
                    schema=coreschema.String(description="入库时间(例如:2025-01-16 16:39:01)"),
                ),
            ]
        elif method.lower() == "delete":
            manual_fields = [
                Field(
                    name="id",
                    required=True,
                    location="query",
                    schema=coreschema.Number(description="id"),
                )
            ]
        return manual_fields

class TmsAddInventoryLogQdView(APIView):
    """
    类说明：
        用于处理tms_add_inventory_log_qd表的数据，可以使用get，post，patch，delete方法
    方法：
        get:获取数据
        post:添加数据
        patch:部分更新数据
        delete:删除数据
    """

    # 设置默认的每页默认数据量
    page_size = 100000
    # 设置在API DOC的字段显示
    schema = TmsAddInventoryQdCustomSchema()

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = TmsAddInventoryQdSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def post(self, request, *args, **kwargs):
        request.data["add_inventory_time"] = datetime.now()
        serializer = TmsAddInventoryQdSerializer(data=request.data)

        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def patch(self, request, *args, **kwargs):
        id = request.data["id"]
        try:
            instance = TmsAddInventoryLogQd.objects.get(id=id)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=404)

        serializer = TmsAddInventoryQdSerializer(instance, data=request.data, partial=True)
        if serializer.is_valid():
            instance.add_inventory_time = datetime.now()
            serializer.save()
            return Response(serializer.data, status=200)
        return Response(serializer.errors, status=400)

    def delete(self, request, *args, **kwargs):
        id = request.query_params.get("id")
        try:
            instance = TmsAddInventoryLogQd.objects.get(id=id)
            instance.delete()
            return Response({"message": "删除成功！"}, status=status.HTTP_200_OK)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=status.HTTP_404_NOT_FOUND)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        queryset = TmsAddInventoryLogQd.objects.filter().order_by("id")
        # 获取请求的参数
        id = request.query_params.get("id")
        prline = request.query_params.get("prline")
        supplier = request.query_params.get("supplier")
        tool_number = request.query_params.get("tool_number")
        tool_type = request.query_params.get("tool_type")
        process_number = request.query_params.get("process_number")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if prline is not None:
            queryset = queryset.filter(prline=prline)
        if supplier is not None:
            queryset = queryset.filter(supplier=supplier)
        if tool_number is not None:
            queryset = queryset.filter(tool_number=tool_number)
        if tool_type is not None:
            queryset = queryset.filter(tool_type=tool_type)
        if process_number is not None:
            queryset = queryset.filter(process_number=process_number)
        else:
            pass
        return queryset

# 出库记录表视图
class TmsOutInventoryQdCustomSchema(AutoSchema):
    """
    自定义的Schema类，用于处理TmsOutInventoryQd相关的序列化逻辑。

    这个类继承自AutoSchema，并根据需要重写或添加方法以处理特定的序列化需求。

    示例用法：
    schema = TmsInventoryCustomSchema()
    serialized_data = schema.dump(data)
    """

    def get_manual_fields(self, path, method):
        # 获取父类中已有的手动字段
        manual_fields = super().get_manual_fields(path, method)

        if method.lower() == "get":
            # 为 GET 请求添加查询参数
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="query",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="process_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="工序号"),
                ),
                Field(
                    name="storage_location",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="仓库位置"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="out_inventory_time", 
                    required=False, 
                    location="query", 
                    schema=coreschema.String(description="出库时间(例如:2025-01-16 16:39:01)"),
                ),
                # 更多查询参数...
            ]
        elif method.lower() == "post":
            # 为 POST 请求设置必填项和其他选项
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="process_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="工序号"),
                ),
                Field(
                    name="storage_location",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="仓库位置"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="add_inventory_time", 
                    required=False, 
                    location="form", 
                    schema=coreschema.String(description="入库时间(例如:2025-01-16 16:39:01)"),
                ),
            ]
        elif method.lower() == "patch":
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="process_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="工序号"),
                ),
                Field(
                    name="storage_location",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="仓库位置"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="add_inventory_time", 
                    required=False, 
                    location="form", 
                    schema=coreschema.String(description="入库时间(例如:2025-01-16 16:39:01)"),
                ),
            ]
        elif method.lower() == "delete":
            manual_fields = [
                Field(
                    name="id",
                    required=True,
                    location="query",
                    schema=coreschema.Number(description="id"),
                )
            ]
        return manual_fields

class TmsOutInventoryLogQdView(APIView):
    """
    类说明：
        用于处理tms_out_inventory_log_qd表的数据，可以使用get，post，patch，delete方法
    方法：
        get:获取数据
        post:添加数据
        patch:部分更新数据
        delete:删除数据
    """

    # 设置默认的每页默认数据量
    page_size = 100000
    # 设置在API DOC的字段显示
    schema = TmsOutInventoryQdCustomSchema()

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = TmsOutInventoryQdSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def post(self, request, *args, **kwargs):
        request.data["add_inventory_time"] = datetime.now()
        serializer = TmsOutInventoryQdSerializer(data=request.data)

        if serializer.is_valid():
            serializer.save()
            instance = TmsOutInventoryLogQd.objects.get(id=serializer.data["id"])
            instance.out_inventory_time = datetime.now()
            instance.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def patch(self, request, *args, **kwargs):
        id = request.data["id"]
        try:
            instance = TmsOutInventoryLogQd.objects.get(id=id)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=404)

        serializer = TmsOutInventoryQdSerializer(instance, data=request.data, partial=True)
        if serializer.is_valid():
            instance.add_inventory_time = datetime.now()
            serializer.save()
            return Response(serializer.data, status=200)
        return Response(serializer.errors, status=400)

    def delete(self, request, *args, **kwargs):
        id = request.query_params.get("id")
        try:
            instance = TmsOutInventoryLogQd.objects.get(id=id)
            instance.delete()
            return Response({"message": "删除成功！"}, status=status.HTTP_200_OK)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=status.HTTP_404_NOT_FOUND)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        queryset = TmsOutInventoryLogQd.objects.filter().order_by("id")
        # 获取请求的参数
        id = request.query_params.get("id")
        prline = request.query_params.get("prline")
        supplier = request.query_params.get("supplier")
        tool_number = request.query_params.get("tool_number")
        tool_type = request.query_params.get("tool_type")
        process_number = request.query_params.get("process_number")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if prline is not None:
            queryset = queryset.filter(prline=prline)
        if supplier is not None:
            queryset = queryset.filter(supplier=supplier)
        if tool_number is not None:
            queryset = queryset.filter(tool_number=tool_number)
        if tool_type is not None:
            queryset = queryset.filter(tool_type=tool_type)
        if process_number is not None:
            queryset = queryset.filter(process_number=process_number)
        else:
            pass
        return queryset

# 物料信息表视图
class TmsMaterialInfoQdCustomSchema(AutoSchema):
    """
    自定义的Schema类，用于处理TmsMaterialInfoQd相关的序列化逻辑。

    这个类继承自AutoSchema，并根据需要重写或添加方法以处理特定的序列化需求。

    示例用法：
    schema = TmsInventoryCustomSchema()
    serialized_data = schema.dump(data)
    """

    def get_manual_fields(self, path, method):
        # 获取父类中已有的手动字段
        manual_fields = super().get_manual_fields(path, method)

        if method.lower() == "get":
            # 为 GET 请求添加查询参数
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="query",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="purchase_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="采购号"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="material_add_time", 
                    required=False, 
                    location="query", 
                    schema=coreschema.String(description="物料信息添加时间(例如:2025-01-16 16:39:01)"),
                ),
                # 更多查询参数...
            ]
        elif method.lower() == "post":
            # 为 POST 请求设置必填项和其他选项
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="purchase_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="采购号"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="material_add_time", 
                    required=False,
                    location="form",
                    schema=coreschema.String(description="物料信息添加时间(例如:2025-01-16 16:39:01)"),
                ),
            ]
        elif method.lower() == "patch":
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="purchase_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="采购号"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="material_add_time", 
                    required=False,
                    location="form",
                    schema=coreschema.String(description="物料信息添加时间(例如:2025-01-16 16:39:01)"),
                ),
            ]
        elif method.lower() == "delete":
            manual_fields = [
                Field(
                    name="id",
                    required=True,
                    location="query",
                    schema=coreschema.Number(description="id"),
                )
            ]
        return manual_fields

class TmsMaterialInfoQdView(APIView):
    """
    类说明：
        用于处理TmsMaterialInfoQd表的数据，可以使用get，post，patch，delete方法
    方法：
        get:获取数据
        post:添加数据
        patch:部分更新数据
        delete:删除数据
    """

    # 设置默认的每页默认数据量
    page_size = 100000
    # 设置在API DOC的字段显示
    schema = TmsMaterialInfoQdCustomSchema()

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = TmsMaterialInfoQdSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def post(self, request, *args, **kwargs):
        request.data["add_inventory_time"] = datetime.now()
        serializer = TmsMaterialInfoQdSerializer(data=request.data)

        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def patch(self, request, *args, **kwargs):
        id = request.data["id"]
        try:
            instance = TmsMaterialInfoQd.objects.get(id=id)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=404)

        serializer = TmsMaterialInfoQdSerializer(instance, data=request.data, partial=True)
        if serializer.is_valid():
            instance.material_add_time = datetime.now()
            serializer.save()
            return Response(serializer.data, status=200)
        return Response(serializer.errors, status=400)

    def delete(self, request, *args, **kwargs):
        id = request.query_params.get("id")
        try:
            instance = TmsMaterialInfoQd.objects.get(id=id)
            instance.delete()
            return Response({"message": "删除成功！"}, status=status.HTTP_200_OK)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=status.HTTP_404_NOT_FOUND)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        queryset = TmsMaterialInfoQd.objects.filter().order_by("id")
        # 获取请求的参数
        id = request.query_params.get("id")
        prline = request.query_params.get("prline")
        purchase_number=request.query_params.get("purchase_number")
        supplier = request.query_params.get("supplier")
        tool_number = request.query_params.get("tool_number")
        tool_life = request.query_params.get("tool_life")
        tool_type = request.query_params.get("tool_type")
        teeth_number = request.query_params.get("teeth_number")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if prline is not None:
            queryset = queryset.filter(prline=prline)
        if purchase_number is not None:
            queryset = queryset.filter(purchase_number=purchase_number)
        if supplier is not None:
            queryset = queryset.filter(supplier=supplier)
        if tool_number is not None:
            queryset = queryset.filter(tool_number=tool_number)
        if tool_life is not None:
            queryset = queryset.filter(tool_life=tool_life)
        if tool_type is not None:
            queryset = queryset.filter(tool_type=tool_type)
        if teeth_number is not None:
            queryset = queryset.filter(teeth_number=teeth_number)
        else:
            pass
        return queryset

# 采购表视图

class TmsPurchaseListQdCustomSchema(AutoSchema):
    """
    自定义的Schema类，用于处理TmsPurchaseListQd相关的序列化逻辑。

    这个类继承自AutoSchema，并根据需要重写或添加方法以处理特定的序列化需求。
    """

    def get_manual_fields(self, path, method):
        # 获取父类中已有的手动字段
        manual_fields = super().get_manual_fields(path, method)

        if method.lower() == "get":
            # 为 GET 请求添加查询参数
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="query",
                    schema=coreschema.Number(description="id"),
                ),
                
                Field(
                    name="supplier",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="purchase_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="采购号"),
                ),
                Field(
                    name="bak_purchase_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="备用采购号"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具型号"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="lead_time",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="交货周期"),
                ),
                Field(
                    name="safety_factor",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="安全系数"),
                ),
                Field(
                    name="approval_time",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="审批时间"),
                ),Field(
                    name="m_replace_q",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="月更换数量"),
                ),
                Field(
                    name="min_invent_q",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="最小库存数量"),
                ),
                Field(
                    name="actual_tool_life",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="实际刀具寿命"),
                ),
                Field(
                    name="purchase_time", 
                    required=False, 
                    location="query", 
                    schema=coreschema.String(description="发起采购时间(例如:2025-01-16 16:39:01)"),
                ),
                Field(
                    name="purchase_quantity",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="采购数量"),
                ),
                Field(
                    name="purchase_status",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="采购状态"),
                ),
                
                # 更多查询参数...
            ]
        elif method.lower() == "post":
            # 为 POST 请求设置必填项和其他选项
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                
                Field(
                    name="supplier",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="purchase_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="采购号"),
                ),
                Field(
                    name="bak_purchase_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备用采购号"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具型号"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="lead_time",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="交货周期"),
                ),
                Field(
                    name="safety_factor",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="安全系数"),
                ),
                Field(
                    name="approval_time",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="审批时间"),
                ),Field(
                    name="m_replace_q",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="月更换数量"),
                ),
                Field(
                    name="min_invent_q",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="最小库存数量"),
                ),
                Field(
                    name="actual_tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="实际刀具寿命"),
                ),
                Field(
                    name="purchase_time", 
                    required=False, 
                    location="form", 
                    schema=coreschema.String(description="发起采购时间(例如:2025-01-16 16:39:01)"),
                ),
                Field(
                    name="purchase_quantity",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="采购数量"),
                ),
                Field(
                    name="purchase_status",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="采购状态"),
                ),
            ]
        elif method.lower() == "patch":
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                
                Field(
                    name="supplier",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="purchase_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="采购号"),
                ),
                Field(
                    name="bak_purchase_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备用采购号"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具型号"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="lead_time",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="交货周期"),
                ),
                Field(
                    name="safety_factor",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="安全系数"),
                ),
                Field(
                    name="approval_time",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="审批时间"),
                ),Field(
                    name="m_replace_q",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="月更换数量"),
                ),
                Field(
                    name="min_invent_q",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="最小库存数量"),
                ),
                Field(
                    name="actual_tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="实际刀具寿命"),
                ),
                Field(
                    name="purchase_time", 
                    required=False, 
                    location="form", 
                    schema=coreschema.String(description="发起采购时间(例如:2025-01-16 16:39:01)"),
                ),
                Field(
                    name="purchase_quantity",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="采购数量"),
                ),
                Field(
                    name="purchase_status",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="采购状态"),
                ),
            ]
        elif method.lower() == "delete":
            manual_fields = [
                Field(
                    name="id",
                    required=True,
                    location="query",
                    schema=coreschema.Number(description="id"),
                )
            ]
        return manual_fields

class TmsPurchaseListQdView(APIView):
    """
    类说明：
        用于处理TmsPurchaseListQd表的数据，可以使用get，post，patch，delete方法
    方法：
        get:获取数据
        post:添加数据
        patch:部分更新数据
        delete:删除数据
    """

    # 设置默认的每页默认数据量
    page_size = 100000
    # 设置在API DOC的字段显示
    schema = TmsPurchaseListQdCustomSchema()

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = TmsPurchaseListQdSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def post(self, request, *args, **kwargs):
        request.data["purchase_time"] = datetime.now()
        serializer = TmsPurchaseListQdSerializer(data=request.data)

        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def patch(self, request, *args, **kwargs):
        id = request.data["id"]
        try:
            instance = TmsPurchaseListQd.objects.get(id=id)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=404)

        serializer = TmsPurchaseListQdSerializer(instance, data=request.data, partial=True)
        if serializer.is_valid():
            # instance.material_add_time = datetime.now()
            serializer.save()
            return Response(serializer.data, status=200)
        return Response(serializer.errors, status=400)

    def delete(self, request, *args, **kwargs):
        id = request.query_params.get("id")
        try:
            instance = TmsPurchaseListQd.objects.get(id=id)
            instance.delete()
            return Response({"message": "删除成功！"}, status=status.HTTP_200_OK)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=status.HTTP_404_NOT_FOUND)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        queryset = TmsPurchaseListQd.objects.filter().order_by("id")
        # 获取请求的参数
        id = request.query_params.get("id")
        supplier = request.query_params.get("supplier")
        purchase_number=request.query_params.get("purchase_number")
        bak_purchase_number = request.query_params.get("bak_purchase_number")
        tool_number = request.query_params.get("tool_number")
        tool_type = request.query_params.get("tool_type")
        tool_life = request.query_params.get("tool_life")
        tool_status = request.query_params.get("tool_status")
        cutting_edge = request.query_params.get("cutting_edge")
        teeth_number = request.query_params.get("teeth_number")
        purchase_status = request.query_params.get("purchase_status")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if supplier is not None:
            queryset = queryset.filter(supplier=supplier)
        if purchase_number is not None:
            queryset = queryset.filter(purchase_number=purchase_number)
        if bak_purchase_number is not None:
            queryset = queryset.filter(bak_purchase_number=bak_purchase_number)
        if tool_number is not None:
            queryset = queryset.filter(tool_number=tool_number)
        if tool_type is not None:
            queryset = queryset.filter(tool_type=tool_type)
        if tool_life is not None:
            queryset = queryset.filter(tool_life=tool_life)
        if tool_status is not None:
            queryset = queryset.filter(tool_status=tool_status)
        if cutting_edge is not None:
            queryset = queryset.filter(cutting_edge=cutting_edge)
        if teeth_number is not None:
            queryset = queryset.filter(teeth_number=teeth_number)
        if purchase_status is not None:
            queryset = queryset.filter(purchase_status=purchase_status)
        else:
            pass
        return queryset


#↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑青岛模块视图函数↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑


# ↓↓↓↓↓↓↓↓↓↓↓↓↓↓宝骏模块视图函数↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

# tms系统库存管理表视图

class TmsInventoryBjCustomSchema(AutoSchema):
    """
    自定义的Schema类，用于处理TmsInventoryBj相关的序列化逻辑。

    这个类继承自AutoSchema，并根据需要重写或添加方法以处理特定的序列化需求。

    示例用法：
    schema = TmsInventoryCustomSchema()
    serialized_data = schema.dump(data)
    """

    def get_manual_fields(self, path, method):
        # 获取父类中已有的手动字段
        manual_fields = super().get_manual_fields(path, method)

        if method.lower() == "get":
            # 为 GET 请求添加查询参数
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="query",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="process_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="工序号"),
                ),
                Field(
                    name="storage_location",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="仓库位置"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="add_inventory_time", 
                    required=False, 
                    location="query", 
                    schema=coreschema.String(description="入库时间(例如:2025-01-16 16:39:01)"),
                ),
                # 更多查询参数...
            ]
        elif method.lower() == "post":
            # 为 POST 请求设置必填项和其他选项
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="process_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="工序号"),
                ),
                Field(
                    name="storage_location",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="仓库位置"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="add_inventory_time", 
                    required=False, 
                    location="form", 
                    schema=coreschema.String(description="入库时间(例如:2025-01-16 16:39:01)"),
                ),
            ]
        elif method.lower() == "patch":
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="process_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="工序号"),
                ),
                Field(
                    name="storage_location",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="仓库位置"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="add_inventory_time", 
                    required=False, 
                    location="form", 
                    schema=coreschema.String(description="入库时间(例如:2025-01-16 16:39:01)"),
                ),
            ]
        elif method.lower() == "delete":
            manual_fields = [
                Field(
                    name="id",
                    required=True,
                    location="query",
                    schema=coreschema.Number(description="id"),
                )
            ]
        return manual_fields

class TmsInventoryBjView(APIView):
    """
    类说明：
        用于处理TmsInventoryBj表的数据，可以使用get，post，patch，delete方法
    方法：
        get:获取数据
        post:添加数据
        patch:部分更新数据
        delete:删除数据
    """

    # 设置默认的每页默认数据量
    page_size = 100000
    # 设置在API DOC的字段显示
    schema = TmsInventoryBjCustomSchema()

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = TmsInventoryBjSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def post(self, request, *args, **kwargs):
        request.data["add_inventory_time"] = datetime.now()
        serializer = TmsInventoryBjSerializer(data=request.data)

        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def patch(self, request, *args, **kwargs):
        id = request.data["id"]
        print(request.data)
        try:
            instance = TmsInventoryBj.objects.get(id=id)
            print(instance)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=404)

        serializer = TmsInventoryBjSerializer(instance, data=request.data, partial=True)
        if serializer.is_valid():
            print("有效")
            serializer.save()
            instance.last_out_invent_time = datetime.now()
            instance.save()
            return Response(serializer.data, status=200)
        else :
            print("无效")
            return Response(serializer.errors, status=400)

    def delete(self, request, *args, **kwargs):
        id = request.query_params.get("id")
        try:
            instance = TmsInventoryBj.objects.get(id=id)
            instance.delete()
            return Response({"message": "删除成功！"}, status=status.HTTP_200_OK)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=status.HTTP_404_NOT_FOUND)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        queryset = TmsInventoryBj.objects.filter().order_by("id")
        # 获取请求的参数
        id = request.query_params.get("id")
        prline = request.query_params.get("prline")
        supplier = request.query_params.get("supplier")
        tool_number = request.query_params.get("tool_number")
        tool_type = request.query_params.get("tool_type")
        tool_status = request.query_params.get("tool_status")
        process_number = request.query_params.get("process_number")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if prline is not None:
            queryset = queryset.filter(prline=prline)
        if supplier is not None:
            queryset = queryset.filter(supplier=supplier)
        if tool_number is not None:
            queryset = queryset.filter(tool_number=tool_number)
        if tool_type is not None:
            queryset = queryset.filter(tool_type=tool_type)
        if tool_status is not None:
            queryset = queryset.filter(tool_status=tool_status)
        if process_number is not None:
            queryset = queryset.filter(process_number=process_number)
        else:
            pass
        return queryset

# 入库记录表视图

class TmsAddInventoryBjCustomSchema(AutoSchema):
    """
    自定义的Schema类，用于处理TmsInventoryBj相关的序列化逻辑。

    这个类继承自AutoSchema，并根据需要重写或添加方法以处理特定的序列化需求。

    示例用法：
    schema = TmsInventoryCustomSchema()
    serialized_data = schema.dump(data)
    """

    def get_manual_fields(self, path, method):
        # 获取父类中已有的手动字段
        manual_fields = super().get_manual_fields(path, method)

        if method.lower() == "get":
            # 为 GET 请求添加查询参数
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="query",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="process_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="工序号"),
                ),
                Field(
                    name="storage_location",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="仓库位置"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="add_inventory_time", 
                    required=False, 
                    location="query", 
                    schema=coreschema.String(description="入库时间(例如:2025-01-16 16:39:01)"),
                ),
                # 更多查询参数...
            ]
        elif method.lower() == "post":
            # 为 POST 请求设置必填项和其他选项
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="process_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="工序号"),
                ),
                Field(
                    name="storage_location",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="仓库位置"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="add_inventory_time", 
                    required=False, 
                    location="form", 
                    schema=coreschema.String(description="入库时间(例如:2025-01-16 16:39:01)"),
                ),
            ]
        elif method.lower() == "patch":
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="process_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="工序号"),
                ),
                Field(
                    name="storage_location",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="仓库位置"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="add_inventory_time", 
                    required=False, 
                    location="form", 
                    schema=coreschema.String(description="入库时间(例如:2025-01-16 16:39:01)"),
                ),
            ]
        elif method.lower() == "delete":
            manual_fields = [
                Field(
                    name="id",
                    required=True,
                    location="query",
                    schema=coreschema.Number(description="id"),
                )
            ]
        return manual_fields

class TmsAddInventoryLogBjView(APIView):
    """
    类说明：
        用于处理tms_add_inventory_log_bj表的数据，可以使用get，post，patch，delete方法
    方法：
        get:获取数据
        post:添加数据
        patch:部分更新数据
        delete:删除数据
    """

    # 设置默认的每页默认数据量
    page_size = 100000
    # 设置在API DOC的字段显示
    schema = TmsAddInventoryBjCustomSchema()

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = TmsAddInventoryBjSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def post(self, request, *args, **kwargs):
        request.data["add_inventory_time"] = datetime.now()
        serializer = TmsAddInventoryBjSerializer(data=request.data)

        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def patch(self, request, *args, **kwargs):
        id = request.data["id"]
        try:
            instance = TmsAddInventoryLogBj.objects.get(id=id)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=404)

        serializer = TmsAddInventoryBjSerializer(instance, data=request.data, partial=True)
        if serializer.is_valid():
            instance.add_inventory_time = datetime.now()
            serializer.save()
            return Response(serializer.data, status=200)
        return Response(serializer.errors, status=400)

    def delete(self, request, *args, **kwargs):
        id = request.query_params.get("id")
        try:
            instance = TmsAddInventoryLogBj.objects.get(id=id)
            instance.delete()
            return Response({"message": "删除成功！"}, status=status.HTTP_200_OK)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=status.HTTP_404_NOT_FOUND)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        queryset = TmsAddInventoryLogBj.objects.filter().order_by("id")
        # 获取请求的参数
        id = request.query_params.get("id")
        prline = request.query_params.get("prline")
        supplier = request.query_params.get("supplier")
        tool_number = request.query_params.get("tool_number")
        tool_type = request.query_params.get("tool_type")
        process_number = request.query_params.get("process_number")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if prline is not None:
            queryset = queryset.filter(prline=prline)
        if supplier is not None:
            queryset = queryset.filter(supplier=supplier)
        if tool_number is not None:
            queryset = queryset.filter(tool_number=tool_number)
        if tool_type is not None:
            queryset = queryset.filter(tool_type=tool_type)
        if process_number is not None:
            queryset = queryset.filter(process_number=process_number)
        else:
            pass
        return queryset

# 出库记录表视图
class TmsOutInventoryBjCustomSchema(AutoSchema):
    """
    自定义的Schema类，用于处理TmsInventoryBj相关的序列化逻辑。

    这个类继承自AutoSchema，并根据需要重写或添加方法以处理特定的序列化需求。

    示例用法：
    schema = TmsInventoryCustomSchema()
    serialized_data = schema.dump(data)
    """

    def get_manual_fields(self, path, method):
        # 获取父类中已有的手动字段
        manual_fields = super().get_manual_fields(path, method)

        if method.lower() == "get":
            # 为 GET 请求添加查询参数
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="query",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="process_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="工序号"),
                ),
                Field(
                    name="storage_location",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="仓库位置"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="out_inventory_time", 
                    required=False, 
                    location="query", 
                    schema=coreschema.String(description="出库时间(例如:2025-01-16 16:39:01)"),
                ),
                # 更多查询参数...
            ]
        elif method.lower() == "post":
            # 为 POST 请求设置必填项和其他选项
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="process_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="工序号"),
                ),
                Field(
                    name="storage_location",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="仓库位置"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="add_inventory_time", 
                    required=False, 
                    location="form", 
                    schema=coreschema.String(description="入库时间(例如:2025-01-16 16:39:01)"),
                ),
            ]
        elif method.lower() == "patch":
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="process_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="工序号"),
                ),
                Field(
                    name="storage_location",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="仓库位置"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="add_inventory_time", 
                    required=False, 
                    location="form", 
                    schema=coreschema.String(description="入库时间(例如:2025-01-16 16:39:01)"),
                ),
            ]
        elif method.lower() == "delete":
            manual_fields = [
                Field(
                    name="id",
                    required=True,
                    location="query",
                    schema=coreschema.Number(description="id"),
                )
            ]
        return manual_fields

class TmsOutInventoryLogBjView(APIView):
    """
    类说明：
        用于处理tms_out_inventory_log_bj表的数据，可以使用get，post，patch，delete方法
    方法：
        get:获取数据
        post:添加数据
        patch:部分更新数据
        delete:删除数据
    """

    # 设置默认的每页默认数据量
    page_size = 100000
    # 设置在API DOC的字段显示
    schema = TmsOutInventoryBjCustomSchema()

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = TmsOutInventoryBjSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def post(self, request, *args, **kwargs):
        request.data["add_inventory_time"] = datetime.now()
        serializer = TmsOutInventoryBjSerializer(data=request.data)

        if serializer.is_valid():
            serializer.save()
            instance = TmsOutInventoryLogBj.objects.get(id=serializer.data["id"])
            instance.out_inventory_time = datetime.now()
            instance.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def patch(self, request, *args, **kwargs):
        id = request.data["id"]
        try:
            instance = TmsOutInventoryLogBj.objects.get(id=id)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=404)

        serializer = TmsOutInventoryBjSerializer(instance, data=request.data, partial=True)
        if serializer.is_valid():
            instance.add_inventory_time = datetime.now()
            serializer.save()
            return Response(serializer.data, status=200)
        return Response(serializer.errors, status=400)

    def delete(self, request, *args, **kwargs):
        id = request.query_params.get("id")
        try:
            instance = TmsOutInventoryLogBj.objects.get(id=id)
            instance.delete()
            return Response({"message": "删除成功！"}, status=status.HTTP_200_OK)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=status.HTTP_404_NOT_FOUND)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        queryset = TmsOutInventoryLogBj.objects.filter().order_by("id")
        # 获取请求的参数
        id = request.query_params.get("id")
        prline = request.query_params.get("prline")
        supplier = request.query_params.get("supplier")
        tool_number = request.query_params.get("tool_number")
        tool_type = request.query_params.get("tool_type")
        process_number = request.query_params.get("process_number")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if prline is not None:
            queryset = queryset.filter(prline=prline)
        if supplier is not None:
            queryset = queryset.filter(supplier=supplier)
        if tool_number is not None:
            queryset = queryset.filter(tool_number=tool_number)
        if tool_type is not None:
            queryset = queryset.filter(tool_type=tool_type)
        if process_number is not None:
            queryset = queryset.filter(process_number=process_number)
        else:
            pass
        return queryset

# 物料信息表视图
class TmsMaterialInfoBjCustomSchema(AutoSchema):
    """
    自定义的Schema类，用于处理TmsMaterialInfoBj相关的序列化逻辑。

    这个类继承自AutoSchema，并根据需要重写或添加方法以处理特定的序列化需求。

    示例用法：
    schema = TmsInventoryCustomSchema()
    serialized_data = schema.dump(data)
    """

    def get_manual_fields(self, path, method):
        # 获取父类中已有的手动字段
        manual_fields = super().get_manual_fields(path, method)

        if method.lower() == "get":
            # 为 GET 请求添加查询参数
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="query",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="purchase_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="采购号"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="material_add_time", 
                    required=False, 
                    location="query", 
                    schema=coreschema.String(description="物料信息添加时间(例如:2025-01-16 16:39:01)"),
                ),
                # 更多查询参数...
            ]
        elif method.lower() == "post":
            # 为 POST 请求设置必填项和其他选项
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="purchase_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="采购号"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="material_add_time", 
                    required=False,
                    location="form",
                    schema=coreschema.String(description="物料信息添加时间(例如:2025-01-16 16:39:01)"),
                ),
            ]
        elif method.lower() == "patch":
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                Field(
                    name="prline",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="生产线"),
                ),
                Field(
                    name="purchase_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="采购号"),
                ),
                Field(
                    name="supplier",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具类型"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="material_add_time", 
                    required=False,
                    location="form",
                    schema=coreschema.String(description="物料信息添加时间(例如:2025-01-16 16:39:01)"),
                ),
            ]
        elif method.lower() == "delete":
            manual_fields = [
                Field(
                    name="id",
                    required=True,
                    location="query",
                    schema=coreschema.Number(description="id"),
                )
            ]
        return manual_fields

class TmsMaterialInfoBjView(APIView):
    """
    类说明：
        用于处理TmsMaterialInfoBj表的数据，可以使用get，post，patch，delete方法
    方法：
        get:获取数据
        post:添加数据
        patch:部分更新数据
        delete:删除数据
    """

    # 设置默认的每页默认数据量
    page_size = 100000
    # 设置在API DOC的字段显示
    schema = TmsMaterialInfoBjCustomSchema()

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = TmsMaterialInfoBjSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def post(self, request, *args, **kwargs):
        request.data["add_inventory_time"] = datetime.now()
        serializer = TmsMaterialInfoBjSerializer(data=request.data)

        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def patch(self, request, *args, **kwargs):
        id = request.data["id"]
        try:
            instance = TmsMaterialInfoBj.objects.get(id=id)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=404)

        serializer = TmsMaterialInfoBjSerializer(instance, data=request.data, partial=True)
        if serializer.is_valid():
            instance.material_add_time = datetime.now()
            serializer.save()
            return Response(serializer.data, status=200)
        return Response(serializer.errors, status=400)

    def delete(self, request, *args, **kwargs):
        id = request.query_params.get("id")
        try:
            instance = TmsMaterialInfoBj.objects.get(id=id)
            instance.delete()
            return Response({"message": "删除成功！"}, status=status.HTTP_200_OK)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=status.HTTP_404_NOT_FOUND)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        queryset = TmsMaterialInfoBj.objects.filter().order_by("id")
        # 获取请求的参数
        id = request.query_params.get("id")
        prline = request.query_params.get("prline")
        purchase_number=request.query_params.get("purchase_number")
        supplier = request.query_params.get("supplier")
        tool_number = request.query_params.get("tool_number")
        tool_life = request.query_params.get("tool_life")
        tool_type = request.query_params.get("tool_type")
        teeth_number = request.query_params.get("teeth_number")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if prline is not None:
            queryset = queryset.filter(prline=prline)
        if purchase_number is not None:
            queryset = queryset.filter(purchase_number=purchase_number)
        if supplier is not None:
            queryset = queryset.filter(supplier=supplier)
        if tool_number is not None:
            queryset = queryset.filter(tool_number=tool_number)
        if tool_life is not None:
            queryset = queryset.filter(tool_life=tool_life)
        if tool_type is not None:
            queryset = queryset.filter(tool_type=tool_type)
        if teeth_number is not None:
            queryset = queryset.filter(teeth_number=teeth_number)
        else:
            pass
        return queryset

# 采购表视图

class TmsPurchaseListBjCustomSchema(AutoSchema):
    """
    自定义的Schema类，用于处理TmsPurchaseListBj相关的序列化逻辑。

    这个类继承自AutoSchema，并根据需要重写或添加方法以处理特定的序列化需求。
    """

    def get_manual_fields(self, path, method):
        # 获取父类中已有的手动字段
        manual_fields = super().get_manual_fields(path, method)

        if method.lower() == "get":
            # 为 GET 请求添加查询参数
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="query",
                    schema=coreschema.Number(description="id"),
                ),
                
                Field(
                    name="supplier",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="purchase_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="采购号"),
                ),
                Field(
                    name="bak_purchase_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="备用采购号"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具型号"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="lead_time",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="交货周期"),
                ),
                Field(
                    name="safety_factor",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="安全系数"),
                ),
                Field(
                    name="approval_time",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="审批时间"),
                ),Field(
                    name="m_replace_q",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="月更换数量"),
                ),
                Field(
                    name="min_invent_q",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="最小库存数量"),
                ),
                Field(
                    name="actual_tool_life",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="实际刀具寿命"),
                ),
                Field(
                    name="purchase_time", 
                    required=False, 
                    location="query", 
                    schema=coreschema.String(description="发起采购时间(例如:2025-01-16 16:39:01)"),
                ),
                Field(
                    name="purchase_quantity",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="采购数量"),
                ),
                Field(
                    name="purchase_status",
                    required=False,
                    location="query",
                    schema=coreschema.String(description="采购状态"),
                ),
                
                # 更多查询参数...
            ]
        elif method.lower() == "post":
            # 为 POST 请求设置必填项和其他选项
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                
                Field(
                    name="supplier",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="purchase_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="采购号"),
                ),
                Field(
                    name="bak_purchase_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备用采购号"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具型号"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="lead_time",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="交货周期"),
                ),
                Field(
                    name="safety_factor",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="安全系数"),
                ),
                Field(
                    name="approval_time",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="审批时间"),
                ),Field(
                    name="m_replace_q",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="月更换数量"),
                ),
                Field(
                    name="min_invent_q",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="最小库存数量"),
                ),
                Field(
                    name="actual_tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="实际刀具寿命"),
                ),
                Field(
                    name="purchase_time", 
                    required=False, 
                    location="form", 
                    schema=coreschema.String(description="发起采购时间(例如:2025-01-16 16:39:01)"),
                ),
                Field(
                    name="purchase_quantity",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="采购数量"),
                ),
                Field(
                    name="purchase_status",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="采购状态"),
                ),
            ]
        elif method.lower() == "patch":
            manual_fields = [
                Field(
                    name="id",
                    required=False,
                    location="form",
                    schema=coreschema.Number(description="id"),
                ),
                
                Field(
                    name="supplier",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="供应商"),
                ),
                Field(
                    name="purchase_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="采购号"),
                ),
                Field(
                    name="bak_purchase_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备用采购号"),
                ),
                Field(
                    name="tool_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具号"),
                ),
                Field(
                    name="tool_type",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具型号"),
                ),
                Field(
                    name="tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具寿命"),
                ),
                Field(
                    name="tool_status",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刀具状态"),
                ),
                Field(
                    name="cutting_edge",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="刃口"),
                ),
                Field(
                    name="teeth_number",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="齿数"),
                ),
                Field(
                    name="notes",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="备注"),
                ),
                Field(
                    name="inventory_quantity",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="库存数量"),
                ),
                Field(
                    name="lead_time",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="交货周期"),
                ),
                Field(
                    name="safety_factor",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="安全系数"),
                ),
                Field(
                    name="approval_time",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="审批时间"),
                ),Field(
                    name="m_replace_q",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="月更换数量"),
                ),
                Field(
                    name="min_invent_q",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="最小库存数量"),
                ),
                Field(
                    name="actual_tool_life",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="实际刀具寿命"),
                ),
                Field(
                    name="purchase_time", 
                    required=False, 
                    location="form", 
                    schema=coreschema.String(description="发起采购时间(例如:2025-01-16 16:39:01)"),
                ),
                Field(
                    name="purchase_quantity",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="采购数量"),
                ),
                Field(
                    name="purchase_status",
                    required=False,
                    location="form",
                    schema=coreschema.String(description="采购状态"),
                ),
            ]
        elif method.lower() == "delete":
            manual_fields = [
                Field(
                    name="id",
                    required=True,
                    location="query",
                    schema=coreschema.Number(description="id"),
                )
            ]
        return manual_fields

class TmsPurchaseListBjView(APIView):
    """
    类说明：
        用于处理TmsPurchaseListBj表的数据，可以使用get，post，patch，delete方法
    方法：
        get:获取数据
        post:添加数据
        patch:部分更新数据
        delete:删除数据
    """

    # 设置默认的每页默认数据量
    page_size = 100000
    # 设置在API DOC的字段显示
    schema = TmsPurchaseListBjCustomSchema()

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = TmsPurchaseListBjSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def post(self, request, *args, **kwargs):
        request.data["purchase_time"] = datetime.now()
        serializer = TmsPurchaseListBjSerializer(data=request.data)

        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def patch(self, request, *args, **kwargs):
        id = request.data["id"]
        try:
            instance = TmsPurchaseListBj.objects.get(id=id)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=404)

        serializer = TmsPurchaseListBjSerializer(instance, data=request.data, partial=True)
        if serializer.is_valid():
            # instance.material_add_time = datetime.now()
            serializer.save()
            return Response(serializer.data, status=200)
        return Response(serializer.errors, status=400)

    def delete(self, request, *args, **kwargs):
        id = request.query_params.get("id")
        try:
            instance = TmsPurchaseListBj.objects.get(id=id)
            instance.delete()
            return Response({"message": "删除成功！"}, status=status.HTTP_200_OK)
        except ObjectDoesNotExist:
            return Response({"error": "Object does not exist"}, status=status.HTTP_404_NOT_FOUND)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        queryset = TmsPurchaseListBj.objects.filter().order_by("id")
        # 获取请求的参数
        id = request.query_params.get("id")
        supplier = request.query_params.get("supplier")
        purchase_number=request.query_params.get("purchase_number")
        bak_purchase_number = request.query_params.get("bak_purchase_number")
        tool_number = request.query_params.get("tool_number")
        tool_type = request.query_params.get("tool_type")
        tool_life = request.query_params.get("tool_life")
        tool_status = request.query_params.get("tool_status")
        cutting_edge = request.query_params.get("cutting_edge")
        teeth_number = request.query_params.get("teeth_number")
        purchase_status = request.query_params.get("purchase_status")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if supplier is not None:
            queryset = queryset.filter(supplier=supplier)
        if purchase_number is not None:
            queryset = queryset.filter(purchase_number=purchase_number)
        if bak_purchase_number is not None:
            queryset = queryset.filter(bak_purchase_number=bak_purchase_number)
        if tool_number is not None:
            queryset = queryset.filter(tool_number=tool_number)
        if tool_type is not None:
            queryset = queryset.filter(tool_type=tool_type)
        if tool_life is not None:
            queryset = queryset.filter(tool_life=tool_life)
        if tool_status is not None:
            queryset = queryset.filter(tool_status=tool_status)
        if cutting_edge is not None:
            queryset = queryset.filter(cutting_edge=cutting_edge)
        if teeth_number is not None:
            queryset = queryset.filter(teeth_number=teeth_number)
        if purchase_status is not None:
            queryset = queryset.filter(purchase_status=purchase_status)
        else:
            pass
        return queryset


#↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑宝骏模块视图函数↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑